package com.hzit.sb2.controller;

import com.hzit.sb2.bean.Student;
import com.hzit.sb2.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StuController {
    @Autowired
    IStudentService studentService;

    /**
     * 查询所有学生
     * @return
     */
    @GetMapping("/")
    public String studentList(Model model,
                              @RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        model.addAttribute("pageInfo",studentService.findAll(page,pageSize));
        return "stuList";
    }

    /**
     * 查询单个学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Student findByPrimaryKey(@PathVariable("id")Integer id){
        return studentService.selectByPrimaryKey(id);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/")
    public int  add(@RequestBody Student student){
        return studentService.insert(student);
    }

    /**
     * 删除一个学生
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public int  delete(@PathVariable("id")Integer id){
        return studentService.deleteByPrimaryKey(id);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    @PutMapping("/")
    public int  update(@RequestBody Student student){
        return studentService.updateByPrimaryKeySelective(student);
    }
}
